/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.gift.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.framework.base.utils.Constants;
import com.boxin.ims.modules.gift.entity.Gift;
import com.boxin.ims.modules.gift.service.GiftService;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 礼品Controller
 * @author Jakemanse
 * @version 2013-07-28
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/gift/gift")
public class GiftController extends BaseController {

	@Autowired
	private GiftService giftService;
	
	@ModelAttribute
	public Gift get(@RequestParam(required=false) Long id) {
		if (id != null){
			return giftService.get(id);
		}else{
			return new Gift();
		}
	}
	
	@RequiresPermissions("gift:gift:view")
	@RequestMapping(value = {"list", ""})
	public String list(Gift gift, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			gift.setUser(user);
		}
        Page<Gift> page = giftService.find(new Page<Gift>(request, response), gift); 
        model.addAttribute("page", page);
        model.addAttribute("qrCodeUrl", QRCodeUtils.QRCODE_JPG_URL_PRIX);
		return "modules/gift/giftList";
	}

	@RequiresPermissions("gift:gift:view")
	@RequestMapping(value = "form")
	public String form(Gift gift, Model model) {
		
		User user = UserUtils.getUser();
		gift.setUser(user);
		
		model.addAttribute("gift", gift);
		return "modules/gift/giftForm";
	}

	@RequiresPermissions("gift:gift:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("file") MultipartFile image,@RequestParam("voiceFile") MultipartFile voiceFile,Gift gift, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, gift)){
			return form(gift, model);
		}
		
		
		/**
		 * 图片
		 */
		if(image != null && image.getOriginalFilename().length()>4){
			String wpPath = Constants.getGiftResourceSavePath();
			String sname = image.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_wp_"+System.currentTimeMillis()+fileType;
			File file = new File(wpPath+fname);
			System.out.println("保存文件:"+wpPath+fname);
			try {
				image.transferTo(file);
				gift.setImgPath(file.getPath());	
			} catch (Exception e) {
				System.out.println("上传文件失败:"+wpPath+fname);
				e.printStackTrace();
			}
			
		}
		
		//保存音频
		if(voiceFile != null && voiceFile.getOriginalFilename().length()>4){
			String giftVoicePath = Constants.getGiftResourceSavePath();
			String sname = voiceFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = "gift_"+UserUtils.getUser().getId()+"_"+System.currentTimeMillis()+fileType;
			File file = new File(giftVoicePath+fname);
			System.out.println("保存文件:"+giftVoicePath+fname);
			try {
				voiceFile.transferTo(file);
				gift.setFilePath(file.getPath());	
			} catch (Exception e) {
				System.out.println("上传文件失败:"+giftVoicePath+fname);
				e.printStackTrace();
			}
			
		}
		
		
		
		
		giftService.save(gift);
		
		addMessage(redirectAttributes, "保存礼品'" + gift.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/gift/gift/?repage";
	}
	
	@RequiresPermissions("gift:gift:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		giftService.delete(id);
		addMessage(redirectAttributes, "删除礼品成功");
		return "redirect:"+Global.ADMIN_PATH+"/gift/gift/?repage";
	}

}
